package com.gjy.aviator.function;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorDecimal;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-03-19 12:23:08
 */
public class TruncFunction extends AbstractFunction {

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
        Number number = arg1.numberValue(env);
        Number places = arg2.numberValue(env);

        BigDecimal db = BigDecimal.valueOf(number.doubleValue());
        BigDecimal bigDecimal = db.setScale(places.intValue(), RoundingMode.DOWN);
        return new AviatorDecimal(bigDecimal);
    }

    @Override
    public String getName() {
        return "TRUNC";
    }

}
